package com.example.smartcity1.service.parklot;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.ParklotPageBean;
import com.example.smartcity1.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class ParklotPageActivity extends AppCompatActivity {

    private TextView parkNameText;
    private ImageView imageView;
    private TextView addressText;
    private TextView distanceText;
    private TextView allParkText;
    private TextView ratesText;
    private TextView vacancyText;
    private TextView priceCapsText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parklot_page);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("停车场详情");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initView();
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 1);
        RetrofitClient.serviceService().getParklotPageData(id).enqueue(new Callback<ParklotPageBean>() {
            @Override
            public void onResponse(Call<ParklotPageBean> call, Response<ParklotPageBean> response) {
                ParklotPageBean body = response.body();
                if (body != null && body.getCode() == 200) {
                    ParklotPageBean.DataDTO data = body.getData();
                    parkNameText.setText(data.getParkName());
                    Glide.with(ParklotPageActivity.this).load(IP + data.getImgUrl())
                            .placeholder(R.drawable.ic_baseline_cloud_download_24)
                            .centerCrop()
                            .into(imageView);
                    addressText.setText(data.getAddress());
                    distanceText.setText("距离 " + data.getDistance() + " 公里");
                    if (Integer.parseInt(data.getAllPark()) - Integer.parseInt(data.getVacancy()) == 0) {
                        allParkText.setText("不对外开放");
                    } else {
                        allParkText.setText("对外开放");
                    }
                    ratesText.setText(data.getRates() + " 元/小时");
                    vacancyText.setText(data.getVacancy() + "个");
                    priceCapsText.setText("每小时"+ data.getRates() +"元， 最高"+ data.getPriceCaps() +"元/天。");
                }
            }

            @Override
            public void onFailure(Call<ParklotPageBean> call, Throwable t) {

            }
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initView() {
        parkNameText = findViewById(R.id.plateNumber_text);
        imageView = findViewById(R.id.imageView);
        addressText = findViewById(R.id.parkName_text);
        distanceText = findViewById(R.id.monetary_text);
        allParkText = findViewById(R.id.allPark_text);
        ratesText = findViewById(R.id.outTime_text);
        vacancyText = findViewById(R.id.entryTime_text);
        priceCapsText = findViewById(R.id.priceCaps_text);
    }
}